home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / windowsformsdemo / textcolorfontform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  11.1 KB  |  263 lines

  1. Public Class TextColorFontForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  26.     Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  27.     Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
  28.     Friend WithEvents cboKnownColors As System.Windows.Forms.ComboBox
  29.     Friend WithEvents lblRed As System.Windows.Forms.Label
  30.     Friend WithEvents lblGreen As System.Windows.Forms.Label
  31.     Friend WithEvents lblBlue As System.Windows.Forms.Label
  32.     Friend WithEvents tbarRed As System.Windows.Forms.TrackBar
  33.     Friend WithEvents tbarBlue As System.Windows.Forms.TrackBar
  34.     Friend WithEvents tbarGreen As System.Windows.Forms.TrackBar
  35.         Friend WithEvents lblColor As System.Windows.Forms.Label
  36.     Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
  37.     Friend WithEvents btnFont As System.Windows.Forms.Button
  38.  
  39.     'Required by the Windows Form Designer
  40.     Private components As System.ComponentModel.Container
  41.  
  42.     'NOTE: The following procedure is required by the Windows Form Designer
  43.     'It can be modified using the Windows Form Designer.  
  44.     'Do not modify it using the code editor.
  45.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  46.         Me.GroupBox1 = New System.Windows.Forms.GroupBox()
  47.         Me.tbarRed = New System.Windows.Forms.TrackBar()
  48.         Me.tbarBlue = New System.Windows.Forms.TrackBar()
  49.         Me.tbarGreen = New System.Windows.Forms.TrackBar()
  50.         Me.lblBlue = New System.Windows.Forms.Label()
  51.         Me.lblGreen = New System.Windows.Forms.Label()
  52.         Me.lblRed = New System.Windows.Forms.Label()
  53.         Me.GroupBox2 = New System.Windows.Forms.GroupBox()
  54.         Me.cboKnownColors = New System.Windows.Forms.ComboBox()
  55.         Me.btnFont = New System.Windows.Forms.Button()
  56.         Me.lblColor = New System.Windows.Forms.Label()
  57.         Me.TextBox1 = New System.Windows.Forms.TextBox()
  58.         Me.FontDialog1 = New System.Windows.Forms.FontDialog()
  59.         Me.GroupBox1.SuspendLayout()
  60.         CType(Me.tbarRed, System.ComponentModel.ISupportInitialize).BeginInit()
  61.         CType(Me.tbarBlue, System.ComponentModel.ISupportInitialize).BeginInit()
  62.         CType(Me.tbarGreen, System.ComponentModel.ISupportInitialize).BeginInit()
  63.         Me.GroupBox2.SuspendLayout()
  64.         Me.SuspendLayout()
  65.         '
  66.         'GroupBox1
  67.         '
  68.         Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.tbarRed, Me.tbarBlue, Me.tbarGreen, Me.lblBlue, Me.lblGreen, Me.lblRed})
  69.         Me.GroupBox1.Location = New System.Drawing.Point(24, 152)
  70.         Me.GroupBox1.Name = "GroupBox1"
  71.         Me.GroupBox1.Size = New System.Drawing.Size(264, 152)
  72.         Me.GroupBox1.TabIndex = 2
  73.         Me.GroupBox1.TabStop = False
  74.         Me.GroupBox1.Text = "FromARGB method"
  75.         '
  76.         'tbarRed
  77.         '
  78.         Me.tbarRed.Location = New System.Drawing.Point(80, 24)
  79.         Me.tbarRed.Maximum = 255
  80.         Me.tbarRed.Name = "tbarRed"
  81.         Me.tbarRed.Size = New System.Drawing.Size(176, 42)
  82.         Me.tbarRed.TabIndex = 1
  83.         Me.tbarRed.TickFrequency = 16
  84.         Me.tbarRed.Value = 255
  85.         '
  86.         'tbarBlue
  87.         '
  88.         Me.tbarBlue.Location = New System.Drawing.Point(80, 107)
  89.         Me.tbarBlue.Maximum = 255
  90.         Me.tbarBlue.Name = "tbarBlue"
  91.         Me.tbarBlue.Size = New System.Drawing.Size(176, 42)
  92.         Me.tbarBlue.TabIndex = 1
  93.         Me.tbarBlue.TickFrequency = 16
  94.         Me.tbarBlue.Value = 255
  95.         '
  96.         'tbarGreen
  97.         '
  98.         Me.tbarGreen.Location = New System.Drawing.Point(80, 66)
  99.         Me.tbarGreen.Maximum = 255
  100.         Me.tbarGreen.Name = "tbarGreen"
  101.         Me.tbarGreen.Size = New System.Drawing.Size(176, 42)
  102.         Me.tbarGreen.TabIndex = 1
  103.         Me.tbarGreen.TickFrequency = 16
  104.         Me.tbarGreen.Value = 255
  105.         '
  106.         'lblBlue
  107.         '
  108.         Me.lblBlue.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  109.         Me.lblBlue.ForeColor = System.Drawing.Color.Blue
  110.         Me.lblBlue.Location = New System.Drawing.Point(8, 112)
  111.         Me.lblBlue.Name = "lblBlue"
  112.         Me.lblBlue.Size = New System.Drawing.Size(56, 16)
  113.         Me.lblBlue.TabIndex = 0
  114.         Me.lblBlue.Text = "Blue"
  115.         Me.lblBlue.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  116.         '
  117.         'lblGreen
  118.         '
  119.         Me.lblGreen.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  120.         Me.lblGreen.ForeColor = System.Drawing.Color.Green
  121.         Me.lblGreen.Location = New System.Drawing.Point(8, 64)
  122.         Me.lblGreen.Name = "lblGreen"
  123.         Me.lblGreen.Size = New System.Drawing.Size(56, 16)
  124.         Me.lblGreen.TabIndex = 0
  125.         Me.lblGreen.Text = "Green"
  126.         Me.lblGreen.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  127.         '
  128.         'lblRed
  129.         '
  130.         Me.lblRed.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  131.         Me.lblRed.ForeColor = System.Drawing.Color.Red
  132.         Me.lblRed.Location = New System.Drawing.Point(8, 24)
  133.         Me.lblRed.Name = "lblRed"
  134.         Me.lblRed.Size = New System.Drawing.Size(56, 16)
  135.         Me.lblRed.TabIndex = 0
  136.         Me.lblRed.Text = "Red"
  137.         Me.lblRed.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  138.         '
  139.         'GroupBox2
  140.         '
  141.         Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboKnownColors})
  142.         Me.GroupBox2.Location = New System.Drawing.Point(24, 72)
  143.         Me.GroupBox2.Name = "GroupBox2"
  144.         Me.GroupBox2.Size = New System.Drawing.Size(264, 72)
  145.         Me.GroupBox2.TabIndex = 4
  146.         Me.GroupBox2.TabStop = False
  147.         Me.GroupBox2.Text = "FromName method"
  148.         '
  149.         'cboKnownColors
  150.         '
  151.         Me.cboKnownColors.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
  152.         Me.cboKnownColors.DropDownWidth = 208
  153.         Me.cboKnownColors.Location = New System.Drawing.Point(24, 32)
  154.         Me.cboKnownColors.Name = "cboKnownColors"
  155.         Me.cboKnownColors.Size = New System.Drawing.Size(224, 26)
  156.         Me.cboKnownColors.TabIndex = 3
  157.         '
  158.         'btnFont
  159.         '
  160.         Me.btnFont.Location = New System.Drawing.Point(304, 24)
  161.         Me.btnFont.Name = "btnFont"
  162.         Me.btnFont.Size = New System.Drawing.Size(112, 40)
  163.         Me.btnFont.TabIndex = 7
  164.         Me.btnFont.Text = "Select Font"
  165.         '
  166.         'lblColor
  167.         '
  168.         Me.lblColor.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  169.         Me.lblColor.Location = New System.Drawing.Point(24, 312)
  170.         Me.lblColor.Name = "lblColor"
  171.         Me.lblColor.Size = New System.Drawing.Size(264, 24)
  172.         Me.lblColor.TabIndex = 6
  173.         '
  174.         'TextBox1
  175.         '
  176.         Me.TextBox1.Location = New System.Drawing.Point(24, 32)
  177.         Me.TextBox1.Name = "TextBox1"
  178.         Me.TextBox1.Size = New System.Drawing.Size(256, 24)
  179.         Me.TextBox1.TabIndex = 0
  180.         Me.TextBox1.Text = "Sample Text"
  181.         '
  182.         'TextColorFontForm
  183.         '
  184.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  185.         Me.ClientSize = New System.Drawing.Size(440, 349)
  186.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnFont, Me.lblColor, Me.TextBox1, Me.GroupBox1, Me.GroupBox2})
  187.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  188.         Me.Name = "TextColorFontForm"
  189.         Me.Text = "TextColorFontForm"
  190.         Me.GroupBox1.ResumeLayout(False)
  191.         CType(Me.tbarRed, System.ComponentModel.ISupportInitialize).EndInit()
  192.         CType(Me.tbarBlue, System.ComponentModel.ISupportInitialize).EndInit()
  193.         CType(Me.tbarGreen, System.ComponentModel.ISupportInitialize).EndInit()
  194.         Me.GroupBox2.ResumeLayout(False)
  195.         Me.ResumeLayout(False)
  196.  
  197.     End Sub
  198.  
  199. #End Region
  200.  
  201.     Private Sub TextColorFontForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  202.         ' first combo item is special
  203.         cboKnownColors.Items.Add("<not a known color>")
  204.  
  205.         ' fill the combobox with the list of known colors
  206.         Dim colorName As String
  207.         For Each colorName In [Enum].GetNames(GetType(KnownColor))
  208.             cboKnownColors.Items.Add(colorName)
  209.         Next
  210.         ' select the first item
  211.         cboKnownColors.SelectedIndex = 0
  212.     End Sub
  213.  
  214.     ' this event fires when a color scrollbar is moved
  215.  
  216.     Private Sub ColorBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbarRed.Scroll, tbarGreen.Scroll, tbarBlue.Scroll
  217.         UpdateControlValues(Color.FromArgb(tbarRed.Value, tbarGreen.Value, tbarBlue.Value))
  218.     End Sub
  219.  
  220.     ' the user has selected a different Known color
  221.  
  222.     Private Sub cboKnownColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboKnownColors.SelectedIndexChanged
  223.         If Not cboKnownColors.Text.StartsWith("<") AndAlso cboKnownColors.Text <> "Transparent" Then
  224.             UpdateControlValues(Color.FromName(cboKnownColors.Text))
  225.         End If
  226.     End Sub
  227.  
  228.     ' update control values
  229.  
  230.     Sub UpdateControlValues(ByVal c As Color)
  231.         TextBox1.BackColor = c
  232.         ' split the color in its three components
  233.         tbarRed.Value = c.R
  234.         tbarGreen.Value = c.G
  235.         tbarBlue.Value = c.B
  236.  
  237.         ' display color components in bottom label
  238.         lblColor.Text = String.Format("Red={0}, Green={1}, Blue={2}", c.R, c.G, c.B)
  239.  
  240.         ' try to assign a known color
  241.         cboKnownColors.Text = c.ToKnownColor.ToString
  242.         ' if it failed, display first element
  243.         If cboKnownColors.Text <> c.ToKnownColor.ToString Then
  244.             cboKnownColors.SelectedIndex = 0
  245.         End If
  246.     End Sub
  247.  
  248.     ' display the Font dialog
  249.  
  250.     Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
  251.         With FontDialog1
  252.             .Font = TextBox1.Font
  253.             If .ShowDialog() = DialogResult.OK Then
  254.                 TextBox1.Font = .Font
  255.             End If
  256.         End With
  257.     End Sub
  258.  
  259.     Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  260.         e.Handled = True
  261.     End Sub
  262. End Class
  263.